Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Destringing a numerical string*variable with existing mixed values

    I have a variable "height" with all the values being numerical but some values are below 50 were captures as "<50" ...metres

    I would like categories this variable to categorical variable "<100" "101-150" ">150"

    I am failing to de-string this variable since some values, I should say are being recognized as non-numeric

    How can destring and recategorize to capture the "<50" values within the <100 category?

    Thanks for your help in advance


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str10 height
    "223" 
    "<50" 
    "103"   
    "77"  
    "88"
    "166"    
    "114"
    "<50"

  • #2
    I have given the new variable the name "ht_cat" but you can, of course give it any name you want; try this:
    Code:
    . gen byte ht_cat=.
    (8 missing values generated)
    
    . replace ht_cat=0 if real(height)<100 | height=="<50"
    (4 real changes made)
    
    . replace ht_cat=1 if inrange(real(height),100, 150)
    (2 real changes made)
    
    . replace ht_cat=2 if real(height)>150 & real(height)<.
    (2 real changes made)
    
    . li, clean noo
    
        height   ht_cat  
           223        2  
           <50        0  
           103        1  
            77        0  
            88        0  
           166        2  
           114        1  
           <50        0  
    
    . la def ht_cat 0 "<100" 1 "101-150" 2 ">150"
    
    . la val ht_cat ht_cat

    Comment


    • #3
      Thanks so much you have indeed cracked it!!!!

      Comment

      Working...
      X